layui.define(['element','form','layer','layedit','upload'],function(exports){
	   var $ = layui.$;
	   var element = layui.element;
	   var layer = layui.layer;
  	   var form = layui.form;
  	   var layedit = layui.layedit;
  	   var url = $('#url').val();
       var admin = layui.admin;
	   
	   window.Detail = function(id){
          location.href= "/default/detail/"+id+"/";
       }
       
       
       
       window.Pay = function(id){
          location.href= "/default/pay/"+id+"/";
       }
       
       
       
       form.on('submit(demo1)', function(data){
           $.post('/default/pay', {name:data.field.name,type:data.field.type,jine:data.field.jine}, function (res) {
                 if(0 === res.code){
                     layer.msg(res.msg, {icon: 1});
                 }else if(1 === res.code){
                     layer.msg(res.msg, {icon: 1});
                 }else if(2 === res.code){
                     layer.msg(res.msg, {icon: 1});
                 }
	       });
	       return false;
       });

       
       layedit.set({
  		  uploadImage: {
  		    url: '/default/edit_upload' //接口url
  		  }
      	});
      	
       var editIndex = layedit.build('eContentEdt'); // 建立编辑器
       
       layui.upload.render({
            elem: '#LAY_video_upload'
            , url: '/default/upload'
            , accept: 'file' //指定允许上传时校验的文件类型
  		     , size: 102400 //最大允许上传的文件大小
            , before: function (obj) {
          	  layer.msg('正在上传中，请稍等...', {icon: 16,offset: 't',shade:0.3,offset: '170px'});
            }
            , done: function (res) {
                if (res.code == 0) {
              	  layer.msg(res.msg, {anim: 6,icon: 6,offset: 't',shade:0.3,offset: '30px',time: 1500}, function(index){
  						//给模板url赋值
  						$("input[name='url']").val("http://localhost:8081/getFileURL/file/"+res.url);
  					});
                } else {
              	  layer.msg(res.msg, {anim: 6,icon: 5,offset: 't',shade: 0.3,offset: '30px',time: 1500});
                }
            }
        });
        
        layui.upload.render({
            elem: '#LAY_image_upload'
            , url: '/default/upload'
            , accept: 'file' //指定允许上传时校验的文件类型
            , exts: 'jpg|png'
  		     , size: 102400 //最大允许上传的文件大小
            , before: function (obj) {
          	  layer.msg('正在上传中，请稍等...', {icon: 16,offset: 't',shade:0.3,offset: '170px'});
            }
            , done: function (res) {
                if (res.code == 0) {
              	  layer.msg(res.msg, {anim: 6,icon: 6,offset: 't',shade:0.3,offset: '30px',time: 1500}, function(index){
  						//给模板url赋值
  						$("input[name='image']").val("http://localhost:8081/getFileURL/file/"+res.url);
  					});
                } else {
              	  layer.msg(res.msg, {anim: 6,icon: 5,offset: 't',shade: 0.3,offset: '30px',time: 1500});
                }
            }
        });
       //TODO 2 /releaseSave
       form.on('submit(demo2)', function(data){
           data.field.content = layedit.getContent(editIndex);
           console.log(data.field.content);
           $.post('/default/releaseSave', {name:data.field.name,content:data.field.content,url:data.field.url,image:data.field.image}, function (res) {
                 if(0 === res.code){
                     layer.msg(res.msg, {icon: 1});
                 }else if(1 === res.code){
                     layer.msg(res.msg, {icon: 1});
                 }else if(2 === res.code){
                     layer.msg(res.msg, {icon: 1});
                 }
	       });
	       return false;
       });

       //TODO: 文章发布Ajax
    form.on('submit(demo3)', function(data){
        data.field.content = layedit.getContent(editIndex);
        console.log(data.field.content);
        $.post('/default/article/save', {name:data.field.name,content:data.field.content,type:data.field.type,image:data.field.image}, function (res) {
            if(0 === res.code){
                layer.msg(res.msg, {icon: 1});

            }else if(1 === res.code){
                layer.msg(res.msg, {icon: 1});
            }else if(2 === res.code){
                layer.msg(res.msg, {icon: 1});
            }
        });
        return false;
    });

    var editIndex1 = layedit.build('eContentEdt1'); // 建立编辑器


    window.SaveRemark = function (id){
       var content = layedit.getContent(editIndex1);
        console.log(id);
        console.log(content);
        $.ajax({
            type:"post",
            url:"/default/saveCourseRemark",
            data:{"id":id,"content":content},
            success:function (res){
                if(0 === res.code){
                    layer.msg(res.msg, {icon: 1});
                    window.setTimeout(window.location.reload(),5000);
                }else if(1 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }else if(2 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }
            }
        });
    }

    window.ReplyCourseRemark = function (id){
        var remarkId = parseInt(id);
        admin.open({
            type: 1,
            area: ['1000px','600px'],
            title: '回复评论',
            content: $('#remarkEdit').html(),
            success: function (layero, dIndex) {
                if(id){
                    $('#eContentEdt').html(id.content);
                }

                var editIndex = layedit.build('remarkContentEdt'); // 建立编辑器

                // 回显表单数据
                form.val('courseEditForm1', id);

                // 表单提交事件
                form.on('submit(replyRemarkDemo)', function (data) {
                    data.field.content = layedit.getContent(editIndex);
                    console.log(data.field.content);
                    console.log(remarkId);
                    var loadIndex = layer.load(2);
                    $.post('/default/replyCourseRemark', {parentId:remarkId,content:data.field.content}, function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.close(dIndex);
                            layer.msg(res.msg, {icon: 1});
                            window.setTimeout(window.location.reload(),5000);
                            //window.location.reload();
                        } else {
                            layer.msg(res.msg, {icon: 2, anim: 6});
                        }
                    });
                    return false;
                });
            }
        });
    }

    //Release部分
    window.SaveReleaseRemark = function (id){
        var content = layedit.getContent(editIndex1);
        console.log(id);
        console.log(content);
        $.ajax({
            type:"post",
            url:"/default/SaveReleaseRemark",
            data:{"id":id,"content":content},
            success:function (res){
                if(0 === res.code){
                    layer.msg(res.msg, {icon: 1});
                    window.setTimeout(window.location.reload(),5000);
                }else if(1 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }else if(2 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }
            }
        });
    }

    window.ReplyReleaseRemark = function (id){
        var remarkId = parseInt(id);
        admin.open({
            type: 1,
            area: ['1000px','600px'],
            title: '回复评论',
            content: $('#remarkEdit').html(),
            success: function (layero, dIndex) {
                if(id){
                    $('#eContentEdt').html(id.content);
                }

                var editIndex = layedit.build('remarkContentEdt'); // 建立编辑器

                // 回显表单数据
                form.val('courseEditForm1', id);

                // 表单提交事件
                form.on('submit(replyRemarkDemo)', function (data) {
                    data.field.content = layedit.getContent(editIndex);
                    console.log(data.field.content);
                    console.log(remarkId);
                    var loadIndex = layer.load(2);
                    $.post('/default/replyReleaseRemark', {parentId:remarkId,content:data.field.content}, function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.close(dIndex);
                            layer.msg(res.msg, {icon: 1});
                            window.setTimeout(window.location.reload(),5000);
                            //window.location.reload();
                        } else {
                            layer.msg(res.msg, {icon: 2, anim: 6});
                        }
                    });
                    return false;
                });
            }
        });
    }

    window.AddReleaseRemarkLikes = function (id){
        console.log(id);
        $.ajax({
            type:"post",
            url:"/default/addReleaseRemarkLikes",
            data:{"id":id,"type":3},
            success:function (res){
                if(0 === res.code){
                    layer.msg(res.msg, {icon: 1});
                    // $("#likes").css("color","red");
                    window.setTimeout(window.location.reload(),5000);
                }else if(1 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }else if(2 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }else if(3 === res.code){
                    layer.msg(res.msg, {icon: 1});
                    // $("#likes").css("color","#99a2aa");
                    window.setTimeout(window.location.reload(),5000);
                }
            }
        });
    }

    window.AddCourseRemarkLikes = function (id){
        console.log(id);
        $.ajax({
            type:"post",
            url:"/default/addCourseRemarkLikes",
            data:{"id":id,"type":2},
            success:function (res){
                if(0 === res.code){
                    layer.msg(res.msg, {icon: 1});
                    // $("#likes").css("color","red");
                    window.setTimeout(window.location.reload(),5000);
                }else if(1 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }else if(2 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }else if(3 === res.code){
                    layer.msg(res.msg, {icon: 1});
                    // $("#likes").css("color","#99a2aa");
                    window.setTimeout(window.location.reload(),5000);
                }
            }
        });
    }


	   exports('detail',null);
})